﻿Ext.apply(Ext.form.VTypes, {
    numberrange: function(val, field) {
        if (!val) {
            return;
        }

        if (field.startNumberField && (!field.numberRangeMax || (val != field.numberRangeMax))) {
            var start = Ext.getCmp(field.startNumberField);

            if (start) {
                start.setMaxValue(val);
                field.numberRangeMax = val;
                start.validate();
            }
        } else if (field.endNumberField && (!field.numberRangeMin || (val != field.numberRangeMin))) {
            var end = Ext.getCmp(field.endNumberField);

            if (end) {
                end.setMinValue(val);
                field.numberRangeMin = val;
                end.validate();
            }
        }

        return true;
    }
});

Ext.apply(Ext.form.VTypes, {
    customText: function(value, field) {
        if (!value) return;

        if (value == '123') {

            return true;
        }
        return false;

    }
});



Ext.apply(Ext.form.VTypes, {
    customText: function(value, field) {
        if (!value) return;

        if (value == '123') {

            return true;
        }
        return false;

    }
});



Ext.apply(Ext.form.VTypes, {
    NumberField_NumberRange: function(value, field) {

        if (value > 10) return true;
        field.markInvalid(String.format('{0} khong hop le ti nao ca', value));
        return false;
    },
    NumberField_NumberRangeText: 'gia tri ko hop le'
});



Ext.apply(Ext.form.VTypes, {
    IsEqualGreater: function(value, field) {

        if (value > 10) return true;
       
        // field.invalidText = "gia tri khong hop le";
        // field.markInvalid('gia tri khong hop le.');
        return false;

    },
   IsEqualGreaterText: 'Gia tri khong hop le, phai lon hon 10'
});

Ext.apply(Ext.form.VTypes, {
    IsString: function(value, field) {
        var newValue = value;
        var newLength = newValue.length;
        var extraChars = ". -,";

        var search;
        for (var i = 0; i != newLength; i++) {
            aChar = newValue.substring(i, i + 1);
            aChar = aChar.toUpperCase();
            search = extraChars.indexOf(aChar);
            if (search == -1 && (aChar < "A" || aChar > "Z")) {
                return false;
            }
        }
        return true;
    },
    IsStringText: 'Data phải là kiểu string'
});

Ext.apply(Ext.form.VTypes, {
    IsInt: function(value, field) {
        return isInteger(value);
    },
    IsIntText: 'Gia tri khong hop le'
});

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function isUnsignedInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}

function isFloat(n) {
    return n === +n && n !== (n | 0);
}

function isDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        alert("Month " + month + " doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}



var TextField_Validator = function(v) {
    if (!v) return "Ô này cần phải nhập giá trị";
    if (v == "Validator") {
        return true;
    }
    return String.format('{0} không hợp lệ', v);
}

Ext.apply(Ext.form.VTypes, {
    TextField_VType: function(v) {
        return v === "VType";
    },
    TextField_VTypeText : "Giá trị không hợp lệ"
});

 



